﻿namespace Red.Toolkit.Profiler.Logic
{
    public enum RawEventType
    {
        BlockStart,
        BlockEnd,
        Signal,
        CounterInc,
        CounterDec,
    };

    public class RawEvent
    {
        private ulong _OrginalTime;
        private ulong _ListTime;
        private double _Time;
        private ProfilerDefinition _Def;
        private RawEventType _Type;
        private uint[] _Data;
        private RawEvent _EndEvent;
        private ProfilerThread _Thread;

        public RawEvent( ulong orgTime, ProfilerDefinition def, RawEventType type, uint[] data, ProfilerThread thread )
        {
            _Def = def;
            _OrginalTime = orgTime;
            _ListTime = orgTime;
            _Time = 0.0;
            _Type = type;
            _Data = data;
            _EndEvent = null;
            _Thread = thread;
        }

        public void ComputeTime( ulong timeBase, ulong timeFreq )
        {
            _Time = (double)((long)_OrginalTime - (long)timeBase) / (double)timeFreq;
        }

        public void LinkEnd( RawEvent endEvent )
        {
            _EndEvent = endEvent;
        }

        public void AdjustTime()
        {
            _ListTime += 1;
        }

        public ulong ListTime { get { return _ListTime; } }
        public double StartTime { get { return _Time; } }
        public double EndTime { get { return (_EndEvent != null) ? (_EndEvent._Time) : _Time; } }
        public RawEvent EndEvent { get { return _EndEvent; } }
        public RawEventType Type { get { return _Type; } }
        public ProfilerDefinition Def { get { return _Def; } }
        public ProfilerThread Thread { get { return _Thread; } }

        public uint Param( int index )
        {
            if ( _Data != null && (index < _Data.Length) )
                return _Data[index];
            else
                return 0;
        }
    }
}
